package com.sheledon.flyCat.core;

import com.sheledon.flyCat.annotation.boot.ComponentScan;
import com.sheledon.flyCat.core.aop.factory.InterceptFactory;
import com.sheledon.flyCat.core.configuration.ConfigurationManager;
import com.sheledon.flyCat.core.ioc.BeanFactory;
import com.sheledon.flyCat.core.factory.RequestRouteMapper;
import com.sheledon.flyCat.core.ioc.DependencyInjection;
import com.sheledon.flyCat.factory.ClassFactory;
import com.sheledon.flyCat.network.socket.server.HttpServer;
/**
 * @author sheledon
 */
public class ApplicationContext {
    private static final ApplicationContext APPLICATION_CONTEXT = new ApplicationContext();

    private ApplicationContext(){}
    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }
    public void run(Class<?> clazz){
        String [] packageNames = getPackageNames(clazz);

        ClassFactory.loadClass(packageNames);


        ConfigurationManager.loadConfigurationFile(clazz);

        ConfigurationManager.loadConfiguration();

        //加载拦截器
        InterceptFactory.loadArrInterceptors(packageNames);

        BeanFactory.loadAllBeans();

        RequestRouteMapper.loadAllRoutes();

        DependencyInjection.inject();

        new HttpServer().start();
    }
    private String [] getPackageNames(Class<?> clazz){
        ComponentScan annotation = clazz.getAnnotation(ComponentScan.class);
        String [] result ;
        return (result=annotation.value()).length==0 ? result
                :new String[]{clazz.getPackage().getName()};
    }
}
